/************************************************************
** Mark Bordignon
** CS 235
** Final Project
** Description:   Menu functions handle display of user input
* 						options and error checking for user input.
**
************************************************************/

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "./studentList.h"

int selection = 0;
studentType *tempStud;

/************************************************************
** function:      invalidEntry
************************************************************/
void invalidEntry()
{
	printf("*** Invalid Entry! ***\n");
   printf("*** Please enter a correct value from the menu options:\n\n");
}

/************************************************************
** function:      studentMenu
************************************************************/
void studentMenu()
{
	bool valid = false;
	int count=0;
	int choice;
	studentType tempStudent;
	char tempFile[12];

	while (!valid){
	   printf("\nEdit Menu:\n");
   	printf("    1. - Add a new student\n");
   	printf("    2. - Edit a student\n");
   	printf("    3. - Delete a student\n");
   	printf("    4. - Display students\n");
   	printf("    5. - Display student grades\n");
   	printf("    6. - Save changes\n");  	
   	printf("    7. - Return to previous menu\n");
   	printf("Select an option: ");
   	if (scanf("%d",&selection)==1) {
   		getchar(); // clear the buffer of the \n
   		switch (selection) {
   			case 1:
   				printf("Enter the students first name: ");
					scanf(" %[^\n]s",tempStudent.firstname);
					printf("Enter the students last name: ");
					scanf(" %[^\n]s",tempStudent.lastname);
					addStudent(tempStudent);
					printf("\nStudent has been added!\n");
					tempStud = getFirstStudent();	
					printStudents(tempStud);
   				break;
   			case 2:
   				tempStud = getFirstStudent();	
					while (1) {
						printf("\nStudent List:\n");
						printStudents(tempStud);
						printf("Select a student by ID: ");
						if (scanf("%d", &choice) == 1) {
							if (checkStdID(tempStud, choice)== true) {
								editStudent(choice);
								printf("\nStudent has been updated!\n");
								break;
							}
							else {
								invalidEntry();
							}
						}
						else {
							invalidEntry();
						}
						getchar();  // clear the \n from the buffer
					}
   				break;
   			case 3:
   				tempStud = getFirstStudent();	
					printf("\nStudent List:\n");
					printStudents(tempStud);
					while (1) {
						printf("Select a student by ID to delete: ");
						if (scanf("%d", &choice) == 1) {
							if (checkStdID(tempStud, choice)== true) {
								delStudent(choice);
								printf("\nStudent has been deleted!\n\n");
								break;
							}
							else {
								invalidEntry();
							}
						}
						else {
							invalidEntry();
						}
						getchar();  // clear the \n from the buffer
					}
   				break;
   			case 4:
   				tempStud = getFirstStudent();	
					printf("\nStudent List:\n");
					printStudents(tempStud);
					printf("\n");
   				break;
   			case 5:
   				tempStud = getFirstStudent();	
					while (1) {
						printf("\nStudent List:\n");
						printStudents(tempStud);
						printf("Select a student ID to show current grades: ");
						if (scanf("%d", &choice) == 1) {
							printf("\n");
							if (checkStdID(tempStud, choice)== true) {
								tempStud = getStudent(choice);
								printGrades(tempStud->studentGrades);
								break;
							}
							else {
								invalidEntry();
							}
						}
						else {
							invalidEntry();
						}
						getchar();  // clear the \n from the buffer
					}
   				break;
   			case 6:
   				/*strcpy(tempFile,"./databases/");
   				strcat(tempFile,fileName);
   				strcat(tempFile,".csv");
   				writeCSV(tempFile);*/
    				writeCSV(fileName);  				
   				break;
   			case 7:
   				valid = true;
   				break;
   			default:
   				invalidEntry();
   		}
   	}
   	else {
	   	getchar(); // clear buffer of \n
   		invalidEntry();
   	}
   }
}

/************************************************************
** function:      gradebookListMenu
************************************************************/
void gradebookListMenu(int size, char gbArray[][30]){
	int i;
	selection = 0;
	bool valid = false;
	
	strcpy(fileName,"");
	printf("Gradebook Menu:\n");
	for (i = 0;i < size; i++){
		printf("   %d. - %s\n",(i+1),&gbArray[i][0]);
	}
	while (!valid) {
		printf("Select a gradebook: ");
		scanf("%d",&selection);
		getchar();  // clear the \n from the buffer
		if (selection > 0 && selection <= size) {
			valid = true;
		}
		else {
			invalidEntry();
		}
	}
	
	// create the file path then
	// call to read the gradebook from the file
	strcat(fileName,"./databases/");
	strcat(fileName,gbArray[selection-1]);
	printf("file to parse: %s\n",fileName);
	parseCSV(fileName);
	
	studentMenu();	
}


